<?php

namespace library;

/**
 * Description of Basket
 *
 * @author Damian
 */
class Basket{

    private $products = array();
    private $productKeys = array();
    private $itemCount = 0;
    private $totalCost = 0;

    public function find(\model\TblProductPicture &$product) {
        $this->productKeys = array_keys($this->products);
        return in_array($product->ProductID->ProductID, $this->productKeys);
    }

    public function add(\model\TblProductPicture &$product) {
        if ($this->find($product)) {
            $this->products[$product->ProductID->ProductID]["Amount"] ++;
            $this->products[$product->ProductID->ProductID]["Cost"]+=$product->ProductID->GrossPrice;            
        } else {
            $this->products[$product->ProductID->ProductID]["Amount"] = 1;
            $this->products[$product->ProductID->ProductID]["Name"] = $product->ProductID->Name;
            $this->products[$product->ProductID->ProductID]["Price"] = $product->ProductID->GrossPrice;
            $this->products[$product->ProductID->ProductID]["Cost"] = $product->ProductID->GrossPrice;
            $this->productKeys = array_keys($this->products);
        }
        $this->countTotalCost();
        $this->countAmount();
    }

    public function countTotalCost() {
        $this->totalCost = 0;
        foreach ($this->products as $product) {
            $this->totalCost+=$product["Cost"];
        }
    }

    public function countAmount() {
        $this->itemCount = 0;
        foreach ($this->products as $product) {
            $this->itemCount +=$product["Amount"];
        }
    }
    public function getItemCount(){
        return $this->itemCount;
    }
    public function getTotalCost(){
        return $this->totalCost;
    }
    public function getProducts(){
        return $this->products;
    }
    public function getProductKeys(){
        return $this->productKeys;
    }

    public function addMultiple(\model\TblProductPicture &$product, $amount) {
        $this->products[$product->ProductID->ProductID]["Amount"] = $amount;
        $this->products[$product->ProductID->ProductID]["Name"] = $product->ProductID->Name;
        $this->products[$product->ProductID->ProductID]["Price"] = $product->ProductID->GrossPrice;
        $this->products[$product->ProductID->ProductID]["Cost"] = $product->ProductID->GrossPrice * $amount;
        $this->productKeys = array_keys($this->products);
        $this->countTotalCost();
        $this->countAmount();
    }

    public function removeSingleItem(\model\TblProductPicture &$product, $all = false) {
        if ($this->find($product)) {
            $this->products[$product->ProductID->ProductID]["Amount"] --;
            $this->products[$product->ProductID->ProductID]["Cost"]-=$product->ProductID->GrossPrice;
            if ($this->products[$product->ProductID->ProductID]["Amount"] <= 0 || $all) {
                unset($this->products[$product->ProductID->ProductID]);
            }
            $this->countTotalCost();
            $this->countAmount();
        }
        $this->productKeys = array_keys($this->products);
    }

    public function clear() {
        $this->products = array();
        $this->productKeys = array_keys($this->products);
        $this->countTotalCost();
        $this->countAmount();
    }

}
